package com.zeedev.flyingsaucer.modules.DownloadModule.impl;

import android.content.Context;
import android.content.Intent;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import com.zeedev.flyingsaucer.beers.downloaders.BeersDownloader;
import com.zeedev.flyingsaucer.modules.BeerDatabaseModule.interfaces.ListModel;
import com.zeedev.flyingsaucer.modules.DownloadModule.interfaces.DownloadManager;
import com.zeedev.flyingsaucer.service.BeerKnurdService2;
import mobi.zeedev.flyingsaucer.common.CredentialsModule.interfaces.IPreferenceCredentials;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * User: James
 * Date: 12/31/10
 * Time: 4:00 PM
 */
@Singleton
public class DownloadManagerImpl implements DownloadManager {

    @Inject
    public DownloadManagerImpl() {
//        hasCredentials = false;
        shouldRefresh = false;
        timeElapsed = false;
    }

//    @Inject
//    BeerKnurdService2 service;

    @Inject
    @Named("autoIPreferenceCredentials")
    Provider<IPreferenceCredentials> preferenceCredentialsProvider;

    @Inject
    Provider<BeersDownloader> beersDownloaderProvider;

    @Inject
    Context mContext;

    @Inject
    ListModel beersModel;

/*    @Inject
    @Named("refreshAllBeerDescriptions")
    Intent beerKnurdServiceIntentProvider;*/

    @Override
    public synchronized void startRefresh() {
        if (getHasCredentials()) {

            //start refresh

            beersDownloaderProvider.get().execute();


//            Intent intent = new Intent(mActivity, BeerKnurdService2.class);
            Intent intent = new Intent();
            intent.putExtra(BeerKnurdService2.SERVICE_COMMAND, BeerKnurdService2.Command.REFRESH_EXTRA_BEER_INFO);
            mContext.startService(intent);
//            service.startService(intent);


//            BeerKnurdService2.startBackgroundGetExtraBeerInfo();

//            if (beersDownloaderProvider.needsRefresh()) {
//                beersDownloaderProvider.get().execute();
//            } else {
//                Toast.makeText(mActivity.get(), "You have already started a refresh.", Toast.LENGTH_SHORT).show();
//            }
        }
    }

    @Override
    public synchronized void startRefreshIfNeeded() {

        if (beersModel.needsRefresh()) {

            startRefresh();

        }

    }

    public enum Properties {
//        HAS_CREDENTIALS,
        SHOULD_REFRESH,
        TIME_ELAPSED
    }

//    Boolean hasCredentials;
    Boolean shouldRefresh;
    Boolean timeElapsed;

    PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    @Override
    public synchronized Boolean getHasCredentials() {
        return preferenceCredentialsProvider.get().getCredentials().isDownloadPairComplete();
//        return hasCredentials;
    }

/*    @Override
    public synchronized void setHasCredentials(Boolean hasCredentials) {
        Boolean oldValue = hasCredentials;
        this.hasCredentials = hasCredentials;
        pcs.firePropertyChange(Properties.HAS_CREDENTIALS.name(), oldValue, hasCredentials);

    }*/

    public synchronized Boolean getShouldRefresh() {
        return shouldRefresh;
    }

    public synchronized void setShouldRefresh(Boolean shouldRefresh) {


        Boolean oldValue = shouldRefresh;
        this.shouldRefresh = shouldRefresh;
        pcs.firePropertyChange(Properties.SHOULD_REFRESH.name(), oldValue, shouldRefresh);
    }

    public synchronized Boolean getTimeElapsed() {
        return timeElapsed;
    }

    public synchronized void setTimeElapsed(Boolean timeElapsed) {
        Boolean oldValue = timeElapsed;
        this.timeElapsed = timeElapsed;
        pcs.firePropertyChange(Properties.TIME_ELAPSED.name(), oldValue, timeElapsed);
    }

    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }

    public synchronized void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(propertyName, listener);
    }

    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(listener);
    }

    public synchronized void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        pcs.removePropertyChangeListener(propertyName, listener);
    }


}
